package ead.cs.service.workflow.executor;

import ead.cs.domain.Alert;
import ead.cs.domain.Ship;
import ead.cs.domain.Shipment;
import ead.cs.domain.workflow.Status;
import ead.cs.service.AlertService;
import ead.cs.service.ShipmentService;
import ead.cs.service.workflow.StatusExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class DelayedItemExecutor implements StatusExecutor<Status> {

    @Autowired
    private AlertService alertService;
    @Autowired
    private ShipmentService shipmentService;

    @Override
    public void execute(String itemId) {
        //business logic to execute in DELAYED
        Shipment shipment = shipmentService.findOne(itemId);
        shipment.setStatus(getStatus());
        shipmentService.save(shipment);

        Ship ship = shipment.getDeclaration().getSchedule().getShip();

        String message = "Delayed items in " + ship.getName() + " ship";
        String title = ship.getName() + " ship is in danger";
        Alert alert = new Alert(Alert.DANGER,title, message, new Date());
        alertService.save(alert);
    }

    @Override
    public Status getStatus() {
        return Status.DELAYED;
    }
}